< previous page page_428 next page >

Page 428
Private Sub Command1_Click()
   Dim h As Long
   h = OpenEventLog(vbNullString, "Application")
   Debug.Print h
   if h<>0 Then Call CloseEventLog(h)
End Sub
The only difference between this code and the C code is using vbNullString to pass a NULL value to the string parameter. You can't use an empty string ("") because this will pass a pointer to a NULL-terminated string to the function instead of 0. It is important to always include the CloseEventLog function as well. If you fail to close the event log, it will remain open for the life of the process, which in this case is Visual Basic. Not only are open handles wasteful, but they may prevent you from opening the event log when you run the program a second time. When you run this program and click the command button, you'll see a non-zero value displayed in the immediate window. This proves that the event log was opened successfully.
CA Language Designed to Confuse
The author of the original question was perplexed by the casting of pointers. Frankly, I can't blame him. C may be one of the most cryptic languages ever created. In fact, there was once a book called something along the lines of The C Puzzle Book that consisted of nothing but convoluted C code examples that challenged the reader to figure out what they actually did. It was fun, but also a little bit irrelevantno good programmer would ever use that kind of code in a real application. Professional-quality code should be readable.1
Let's take another look at the loop in the C code, but this time I'm going to eliminate some of the parameters and replace them with a description of what is actually going on.
while ( The function ReadEventLog(As many EVENTLOGRECORD structures as will fit
entirely in a buffer) reads one or more structures) {
   while (The number of bytes actually read from the event log > 0) {
      Display information from this record
      dwRead -= pevlr->Length; Subtract the size of this record
      And change the pointer to point to the next record in the buffer
      pevlr = (EVENTLOGRECORD *) ((LPBYTE) pevlr + pevlr->Length);
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
1 This book was inspired by The C Puzzle Book. However, I tried to create puzzles that were relevant to the needs of real VB programmers, while keeping the element of fun that made the original so unique.

 
< previous page page_428 next page >